const api = require('./api/api')
var language = require("./utils/english")
App({
    globalData: {
        eventName: "",
        venueName: "",
        cityName: "",
        location: "",
        navs: [
            { image: "4", name: "Party", url: "../index/index", ico1: '../../images/party(1).png', ico2: '../../images/party(2).png', },
            { image: "1", name: '我的', url: "../my/my", ico1: '../../images/contacts(1).png', ico2: '../../images/contacts(2).png', }
        ],
        activityDetails: {},
        language: language,
        openid: "",
        unionId: "",
        union: {},
        newCount: 0, //新增票券的数量
        ifIndexOnshow: 1 //为0时未选择修改城市，未1时选择修改城市
    },
    getApi: api,
    onLaunch() {
        var that = this
        that.login()
        wx.setStorageSync('isChinese', true)
    },
    login() {
        var that = this
        wx.login({
            success: function(res) {
                var code = res.code
                if (res.code) {
                    wx.getUserInfo({
                        success: res => {
                            // 可以将 res 发送给后台解码出 unionId
                            that.globalData.userInfo = res
                            var params = {
                                    method: "opg.openid.get",
                                    wxAppId: "wx6ff79d8471c55fe4",
                                    wxAppAecret: "472a307104021da09bfe24dafb333c23",
                                    code: code,
                                    encryptedData: res.encryptedData,
                                    iv: res.iv
                                }
                                // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                                // 所以此处加入 callback 以防止这种情况
                            api.getUuid("POST", params).then(res => {
                                if (res.statusCode == 200) {
                                    api.session = res.data.identity.id
                                        //   api.session ="961146070787035136"
                                    that.globalData.unionId = "857573744049524736"
                                    that.globalData.openid = res.data.openid
                                    that.globalData.unionId = res.data.union.id
                                    that.globalData.mobilePhone = res.data.union.mobilePhone
                                    that.globalData.union = res.data.union
                                    that.watch && that.watch()
                                }
                            })
                        },
                        fail(res) {
                            // wx.showModal({
                            //     title: wx.getStorageSync("isChinese") ? '提示' : 'Prompt',
                            //     showCancel: false,
                            //     content: "请点击右上角.../ -关于设置- ... -设置-用户信息中设置"
                            // })
                        }
                    })
                } else {
                    console.log('获取用户登录态失败！' + res.errMsg)
                }
            },
            fail(res) {}
        });
    }
    // imagePath: "http://82.myoyo.cn/"
})